package com.dangdang.serviceImpl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.dangdang.domain.Category;
import com.dangdang.service.CategoryService;
import com.dangdang.util.HbnUtils;

/**
 * @author Wangpeihu
 * @team [Never give up]
 * @since JDK1.6(建议)
 * @date 2010-1-21
 */
public class CategoryServiceImpl implements CategoryService {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.dangdang.service.CategoryService#addCategory(com.dangdang.domain.
	 *      Category)
	 */
	@Override
	public void addCategory(Category category) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.dangdang.service.CategoryService#deleteCategory(com.dangdang.domain
	 *      .Category)
	 */
	@Override
	public void deleteCategory(Category category) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.dangdang.service.CategoryService#findAllCategory()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Category> findAllCategory() {
		Session s = HbnUtils.getSession();
		Query query = s.createQuery("from Category");
		List<Category> categories = query.list();
		s.close();
		return categories;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.dangdang.service.CategoryService#findCategoryById(com.dangdang.domain
	 *      .Category)
	 */
	@Override
	public Category findCategoryById(Category category) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.dangdang.service.CategoryService#updateCategory(com.dangdang.domain
	 *      .Category)
	 */
	@Override
	public void updateCategory(Category category) {
		// TODO Auto-generated method stub

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Category> findAllCategory(Integer id, Integer pid) {
		Session session = HbnUtils.getSession();

		Query query = session
				.createQuery("from Category where id = ? and parentId = ?");
		query.setParameter(0, id);
		query.setParameter(1, pid);
		List<Category> categoryList = query.list();
		session.close();
		return categoryList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Category> findAllCategory(Integer pid) {
		Session session = HbnUtils.getSession();

		Query query = session.createQuery("from Category where parentId = ?");
		query.setParameter(0, pid);
		List<Category> categoryList = query.list();
		session.close();
		return categoryList;
	}

}
